<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="研究方向码">
              <a-input v-model="queryParam.yjfxm"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="研究方向名称">
              <a-input v-model="queryParam.yjfxmc"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select allowClear v-model="queryParam.ssyxm" showSearch :filterOption="filterOption" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="专业层次">
                <a-select allowClear v-model="queryParam.zycc">
                  <a-select-option v-for="i in TYPE0353" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="指导专业">
                <a-select allowClear v-model="queryParam.sszym" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="招生年度">
                <a-select allowClear v-model="queryParam.zsnd">
                  <a-select-option v-for="i in TYPE0387" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

          </template>
          <a-col :md="8" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <!--<a-button type="primary">导出</a-button>-->
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="{alert: false}"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x:1200}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a>
        <a 
          slot="operation" 
          slot-scope="a, b" 
          v-if="(!b.dstjzt || b.dstjzt === '0')" 
          :disabled="b.zsxn !== systemZsnd && selectMenu.yx.length === 1" 
          @click="edit(b)">修改</a>
      </s-table>
    </a-card>

    <teacher-list :visible="visible" :researchInfo="researchInfo" @close="visible = false" @refresh="$refs.table.refresh(false)"/>
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import teacherList from './component/teacherList'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '指导专业码',
          dataIndex: 'sszym'
        },
        {
          title: '专业名称',
          dataIndex: 'sszymc'
        },
        {
          title: '研究方向码',
          dataIndex: 'yjfxm'
        },
        {
          title: '导师研究方向',
          dataIndex: 'yjfxmc'
        },
        {
          title: '专业层次',
          dataIndex: 'xwccmc'
        },
        {
          title: '招生年度',
          dataIndex: 'zsxn'
        },
        {
          title: '导师',
          dataIndex: 'dsmc',
          width:220
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width: 80,
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.teacher.teacherResearchList(params).then(res => {
          res.data.rows.map((item)=>{
            item.yjfxm = item.yjfxm.substring(item.yjfxm.length - 2)
          })
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {
        yx: []
      },
      visible: false,
      researchInfo: {},
      initDone: false,
      systemZsnd: ''
    }
  },
  components: {STable, teacherList},
  computed: {
    ...mapState({
      TYPE0353: state => state.app['TYPE0353'], // 专业层次
      TYPE0387: state => state.app['TYPE0387']
    })
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if ((from.name === '导师详细信息') && pageParams.path === vm.$route.path) {
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
      }
      // vm.initDone = true
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.name === '导师详细信息') {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0353')
    this.$store.dispatch('app/setDictionary', 'TYPE0387')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'ssyxm', this.selectMenu.yx[0].dwh)
          this.yxChange(this.queryParam.ssyxm)
        }
        this.$set(this.queryParam, 'zsnd', res[1].masterAdmissionYear)
        this.systemZsnd = res[1].masterAdmissionYear
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    direct (data) {
      this.$router.push(`/teacher/info/${data.dsbh}`)
    },
    edit (data) {
      this.visible = true
      this.researchInfo = data
    }
  }
}
</script>

<style>

</style>
